﻿using AutoMapper;
using Chloe.SqlServer;
using Infrastructure.Extend;
using Infrastructure.Operator;
using Infrastructure.Web;
using MyData;
using MyData.DB;
using MyData.Dto;
using MyData.Models;
using MyDomain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyApplication
{
    public class AccessRecordApp
    {
        private IMobileRepository _mobileRepository;
        private IUserInfoRepository _userInfoRepository;
        private IAccessRecordRepository _accessRecordRepository;
        private IDepartRepository _departRepository;
        public AccessRecordApp(
            IMobileRepository mobileRepository,
            IUserInfoRepository userInfoRepository,
            IDepartRepository departRepository,
            IAccessRecordRepository accessRecordRepository)
        {
            _mobileRepository = mobileRepository;
            _userInfoRepository = userInfoRepository;
            _departRepository = departRepository;
            _accessRecordRepository = accessRecordRepository;
        }

        public PageDto<AccessRecordDto> GetAccessRecord(QueryListModel query, string NickName, Guid MobileId, DateTime startDate, DateTime endDate, int orderName, int orderType)
        {
            using (var db = new MsSqlContext(DbHelper.ConnectionString))
            {
                var userList = db.Query<UserInfo>();
                if (!string.IsNullOrEmpty(NickName))
                {
                    userList = userList.Where(c => c.NickName.Contains(NickName));
                }

                var mobileList = db.Query<Mobile>().ToList();
                if (MobileId != Guid.Empty)
                {
                    mobileList = mobileList.Where(c => c.Id == MobileId).ToList();
                }
                var departList = db.Query<Depart>().ToList();
                if (OperatorProvider.Provider.GetCurrent().DepartId.HasValue)
                {
                    departList = departList.Where(c => c.Id == OperatorProvider.Provider.GetCurrent().DepartId.Value).ToList();
                }
                var accessRecordList = db.Query<AccessRecord>().
                Where(c => c.UserType == (int)EnumUserType.员工 && c.CreatedTime >= startDate && c.CreatedTime <= endDate).Select(c => new AccessRecord() { Id = c.Id, MobileId = c.MobileId, CreateDate = c.CreateDate, CreatedTime = c.CreatedTime, UserId = c.UserId, Similar = c.Similar, TurnOver = c.TurnOver }).OrderByDesc(c => c.CreatedTime).ToList();


                var linq = (from a in accessRecordList
                            join m in mobileList on a.MobileId equals m.Id
                            join u in userList.ToList() on a.UserId equals u.Id
                            join d in departList on u.DepartId equals d.Id
                            select new AccessRecordDto()
                            {
                                Id = a.Id,
                                NickName = u.NickName,
                                DepartName = d.Name,
                                MobileName = m.Name,
                                CreatedTime = a.CreatedTime,
                                CreateDate = a.CreateDate,
                                RoleName = u.RoleName,
                                TurnOver = a.TurnOver,
                                Similar = a.Similar
                            }).ToList();
                if (orderName == 0)
                {
                    if (orderType == 0)
                    {
                    }
                    else
                    {
                        linq = linq.OrderBy(c => c.CreatedTime).ToList();
                    }
                }
                else if (orderName == 1)
                {
                    if (orderType == 0)
                    {
                        linq = linq.OrderByDescending(c => c.DepartName).ThenByDescending(c => c.CreatedTime).ToList();
                    }
                    else
                    {
                        linq = linq.OrderBy(c => c.DepartName).ThenByDescending(c => c.CreatedTime).ToList();
                    }
                }
                else if (orderName == 2)
                {
                    if (orderType == 0)
                    {
                        linq = linq.OrderByDescending(c => c.NickName).ThenByDescending(c => c.CreatedTime).ToList();
                    }
                    else
                    {
                        linq = linq.OrderBy(c => c.NickName).ThenByDescending(c => c.CreatedTime).ToList();
                    }
                }
                else if (orderName == 3)
                {
                    if (orderType == 0)
                    {
                        linq = linq.OrderByDescending(c => c.MobileName).ThenByDescending(c => c.CreatedTime).ToList();
                    }
                    else
                    {
                        linq = linq.OrderBy(c => c.MobileName).ThenByDescending(c => c.CreatedTime).ToList();
                    }
                }
                WebHelper.WriteSession("Data", linq);
                var result = linq.AsQueryable().Paging(query.Limit, query.Offset);
                return new PageDto<AccessRecordDto>()
                {
                    total = result.total,
                    rows = result.rows
                };
            }
        }


        public PageDto<AccessRecordDto> GetVisitorRecord(QueryListModel query, string NickName, Guid MobileId, DateTime StartDate, DateTime EndDate)
        {
            using (var db = new MsSqlContext(DbHelper.ConnectionString))
            {
                var userList = db.Query<Visitor>();
                if (!string.IsNullOrEmpty(NickName))
                {
                    userList = userList.Where(c => c.Name.Contains(NickName));
                }
                var mobileList = db.Query<Mobile>().ToList();
                if (MobileId != Guid.Empty)
                {
                    mobileList = mobileList.Where(c => c.Id == MobileId).ToList();
                }
                var departList = db.Query<Depart>().ToList();
                Guid? departId = OperatorProvider.Provider.GetCurrent().DepartId;
                if (departId.HasValue)
                {
                    departList = departList.Where(c=>c.Id==departId.Value).ToList();
                }
                var accessRecordList = db.Query<AccessRecord>().Where(c => c.UserType == (int)EnumUserType.访客 && c.CreatedTime >= StartDate && c.CreatedTime <= EndDate).Select(c => new AccessRecord() { Id = c.Id, MobileId = c.MobileId, CreateDate = c.CreateDate, CreatedTime = c.CreatedTime, UserId = c.UserId, Similar = c.Similar }).OrderByDesc(c => c.CreatedTime).ToList();
                var linq = (from a in accessRecordList
                            join m in mobileList on a.MobileId equals m.Id
                            join u in userList.ToList() on a.UserId equals u.Id
                            join d in departList on u.DepartId equals d.Id
                            select new AccessRecordDto()
                            {
                                Id = a.Id,
                                NickName = u.Name,
                                DepartName = d.Name,
                                MobileName = m.Name,
                                CreatedTime = a.CreatedTime,
                                CreateDate = a.CreateDate,
                                RoleName = u.OwnerName,
                                Similar = a.Similar,
                                TurnOver = a.TurnOver
                            }).ToList();
                WebHelper.WriteSession("Data", linq);
                var result = linq.AsQueryable().Paging(query.Limit, query.Offset);
                return new PageDto<AccessRecordDto>()
                {
                    total = result.total,
                    rows = result.rows
                };
            }
        }
        public bool AccessRecordDelete(Guid Id)
        {
            var entity = _accessRecordRepository.FindEntity(Id);
            return _accessRecordRepository.Delete(entity) > 0;
        }
        public bool AccessRecordBatchDelete(List<Guid> ids)
        {
            return _accessRecordRepository.AccessRecordBatchDelete(ids);
        }
        public bool SaveAccessRecord(AccessRecord accessRecord)
        {
            return _accessRecordRepository.SaveAccessRecord(accessRecord);
        }
        public bool SaveAccessRecordRM(AccessRecord accessRecord, string machineCode)
        {
            return _accessRecordRepository.SaveAccessRecordRM(accessRecord, machineCode);
        }
        public bool SaveAccessRecord(List<AccessRecord> accessRecords)
        {
            return _accessRecordRepository.SaveAccessRecords(accessRecords);
        }

        public List<RecordChartDto> GetRecordChartDtos(DateTime StartDate, DateTime EndDate)
        {
            return _accessRecordRepository.GetRecordChartDtos(StartDate, EndDate);
        }
        public PageDto<CheckInDto> GetCheckIn(QueryListModel query, DateTime StartDate, DateTime EndDate)
        {

            return _accessRecordRepository.GetCheckIn(query, StartDate, EndDate);
        }
        public RecentRecordDto GetRencentReord()
        {
            return _accessRecordRepository.GetRecentRecord();
        }
        public List<RecentRecordDto> GetRencentRecord8(Guid mobileId)
        {

            return _accessRecordRepository.GetRecentRecord8(mobileId);
        }
        public bool SaveRecordPic(Guid recordId, string picPath)
        {
            return _accessRecordRepository.SaveRecordPic(recordId, picPath);
        }
        public List<UserRecordPicDto> GetRecordTop20()
        {
            return _accessRecordRepository.GetRecordTop20();
        }
        public PageDto<CheckRecordDto> GetNewAccessRecord(QueryListModel query, string NickName, DateTime StartDate, DateTime EndDate)
        {
            return _accessRecordRepository.GetNewAccessRecord(query, NickName, StartDate, EndDate);
        }
        public AccessRecordDto GetAccessRecord(Guid id)
        {
            var entity = _accessRecordRepository.FindEntity(id);
            return Mapper.Map<AccessRecord, AccessRecordDto>(entity);
        }
        public PageDto<XXCheckRecord> GetXXCheckRecord(QueryListModel query, DateTime StartDate, DateTime EndDate)
        {

            return _accessRecordRepository.GetXXCheckRecord(query, StartDate, EndDate);
        }
    }
}
